//
//  328. 奇偶链表.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/9/6.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation

class Solution328 {
    // 分离后进行合并
    func oddEvenList(_ head: ListNode?) -> ListNode? {
        let vHead1 = ListNode(-1)
        let vHead2 = ListNode(-1)
        var one: ListNode? = vHead1
        var two: ListNode? = vHead2
        var cur: ListNode? = head
        var count = 1
        while cur != nil {
            if count % 2 == 1 {
                one?.next = cur
                one = one?.next
            } else {
                two?.next = cur
                two = two?.next
            }
            count += 1
            cur = cur?.next
        }
        one?.next = vHead2.next
        two?.next = nil
        return vHead1.next
    }
    static func test() {
        let solution = Solution328()
        let node1 = ListNode(1)
        let node2 = ListNode(2)
        let node3 = ListNode(3)
        let node4 = ListNode(4)
        let node5 = ListNode(5)
        
        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        
        let result = solution.oddEvenList(node1)
        print(result)
    }
}
